﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web;
using DHQGHN.DAL.BO.TDT;
using DHQGHN.DAL.Models.DanhMuc;
using DHQGHN.DAL.Models.TDT;

namespace DHQGHN.Services.TDT
{
    public class AreaDSKHHGDService : IAreaDSKHHGDService
    {
        private AREA_DSKHHGD_BO area_DSKHHGD_BO;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(AreaDSKHHGDService));
        #region Constructors
        /// <summary>
        /// 
        /// </summary>
        public AreaDSKHHGDService()
        {
            //string connString = ConfigurationManager.ConnectionStrings["DHQGHN_TDT.ConnectionString"].ConnectionString;
            area_DSKHHGD_BO = new AREA_DSKHHGD_BO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }
        #endregion

        #region Service Methods
        /// <summary>
        /// Datlq, get danh sach dia ban dieu tra theo ma khu vuc hanh chinh de mapping voi dieu tra vien
        /// </summary>
        /// <param name="adminCode"></param>
        /// <returns></returns>
        public IEnumerable<AREA_DSKHHGD> GetByAdminCode_MappingEnum(string adminCode, string areaID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<AREA_DSKHHGD>();
                }
                else
                {
                    return area_DSKHHGD_BO.GetByAdminCode_MappingArea(adminCode, areaID);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    //new FaultReason(string.Format("{0}", "Service fault exception")));
                    new FaultReason(string.Format("xx{0}", ex.Message)));
            }
        }

        /// <summary>
        /// Datlq, get danh sach dia ban dieu tra theo ma khu vuc hanh chinh de mapping voi dieu tra vien
        /// </summary>
        /// <param name="adminCode"></param>
        /// <returns></returns>
        public IEnumerable<AREA_DSKHHGD> GetByAdminCode_NotMappingEnum(string adminCode, string areaID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<AREA_DSKHHGD>();
                }
                else
                {
                    return area_DSKHHGD_BO.GetByAdminCode_NotMappingArea(adminCode, areaID);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    //new FaultReason(string.Format("{0}", "Service fault exception")));
                    new FaultReason(string.Format("xx{0}", ex.Message)));
            }
        }

        public IEnumerable<AREA_DSKHHGD> GetByAreaID(string areaID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<AREA_DSKHHGD>();
                }
                else
                {
                    return area_DSKHHGD_BO.GetByAreaID(areaID);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    //new FaultReason(string.Format("{0}", "Service fault exception")));
                    new FaultReason(string.Format("xx{0}", ex.Message)));
            }
        }
        #endregion
    }
}